package com.xmcc.interceptor;

import com.xmcc.utill.ThreadLocalCommon;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 测试拦截器
 * 越上层的功能越少 但是可扩展性越强
 *
 */
@Slf4j
public class TestInterceptor extends HandlerInterceptorAdapter {


    /**
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     *  调用目标方法前执行,相当于filter中的chain.doFilter
     *  return true;放过
     *  return false;不放过
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("interceptor preHandle 执行了...");
        return true;
    }
    //目标方法 正常执行结束后执行的方法 有异常不执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("interceptor postHandle 执行了...");
    }
    //相当于是finally 表示 正不正常执行目标方法都会执行 有没有都执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("interceptor afterCompletion 执行了...");
        //当前线程解绑
        ThreadLocalCommon.removeHttpServletRequest();
        ThreadLocalCommon.removeSysUser();
    }
}
